<?php

namespace app\admin\controller;

use think\Controller;

class Articles extends Controller
{
    //页面分页
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Articles')->order('weight asc,update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Articles')->order('weight asc,update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $article = $data['data'];
        foreach ($article as $k=>$v){
            $article[$k]['homeShow'] =json_decode($v['homeShow']);

            $article[$k]['listStyle'] =json_decode($v['listStyle']);
            $article[$k]['file'] =json_decode($v['file']);
            $article[$k]['releaseAt'] = date('Y-m-d H:i:s', $article[$k]['releaseAt']);
            $article[$k]['tag']= explode(",",$article[$k]['tag']);

        }
        $return['data'] = $article;
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }
    //添加文章
    public function addArticles(){

        $page = input('post.page');
        $size = input('post.size');

       $tag = input('post.tag');
        $tags ='';
             foreach ($tag as $v){
              $tags =$tags.",".$v;
             }

            $data = [
                'title' => input('post.title'),
                'class_id' =>  input('post.category'),
                'content' => input('post.content'),
                'author' => input('post.author'),
                'tag' => $tags,
                'defaulePageview' => input('post.defaulePageview'),
                'level' => input('post.level'),
                'homeShow' => json_encode(input('post.homeShow')),
                'listStyle' => json_encode(input('post.listStyle')),
                'file' => json_encode(input('post.file')),
                'weight' =>0,
                'releaseAt' => time(input('post.releaseAt')),

            ];
//       return $data;
            $resault = model('Articles')->addArticles($data);
            if ($resault==1){
                $return = $this->page($page,$size,true,"查询成功");
            }else{
                $return = $this->page($page,$size,false,$resault);
            }
        return json($return);

    }
    //删除文章
    public function deleteArticles(){
        $page = input('delete.page');
        $size = input('delete.size');
        $id = input('delete.id');

        $basesInfo = model('Articles')->where('id',$id)->find();

        if (!$basesInfo){
            $return = $this->page($page,$size,false,'查无此文章');
        }else{
            $result = model('Articles')->where('id',$id)->delete();
            if ($result==1){
                $return = $this->page($page,$size,true,'查询成功');
            }else{
                $return = $this->page($page,$size,false,'查询失败');
            }
        }
        return json($return);
    }
    //修改文章
    public function updateArticles(){
        $page = input('put.page');
        $size = input('put.size');
        $tag = input('put.tag');
        $tags ='';
        foreach ($tag as $v){
            $tags =$tags.",".$v;
        }
        $data = [

            'title' => input('put.title'),
            'class_id' =>  input('put.category'),
            'content' => input('put.content'),
            'author' => input('put.author'),
            'tag' => $tags,
            'defaulePageview' => input('put.defaulePageview'),
            'level' => input('put.level'),
            'homeShow' => json_encode(input('put.homeShow')),
            'listStyle' => json_encode(input('put.listStyle')),
            'file' => json_encode(input('put.file')),
            'releaseAt' => time(input('post.releaseAt')),
            'update_time' => time(),

        ];

            $id = input('put.id');
            $basesInfo =model('Articles')->where('id',$id)->find();

            if ($basesInfo){
                $result = model('Articles')->updateArticles($id,$data);
                if ($result){
                    $return = $this->page($page,$size,true,'查询成功');
                }else{
                    $return = $this->page($page,$size,false,$result);
                }
            }else{
                $return = $this->page($page,$size,false,"查无此文章");
            }
        return json($return);

    }
    //文章列表
    public function listeArticles(){
        $size = input('get.size');
        $page = input('get.page');
        $return = $this->page($page,$size,true,"查询成功");

        return json($return);

    }
    //文章详情
    public function contentArticles(){
        $json = [
            'data' => '',
            'success' => false,
            'messages' => '查询文章失败'
        ];

            $id = input('get.id');
            $ArticleInfo = model('Articles')->where('id',$id)->find();


            if ($ArticleInfo){
                $ArticleInfo['homeShow'] =json_decode($ArticleInfo['homeShow']);
                $ArticleInfo['listStyle'] =json_decode($ArticleInfo['listStyle']);
                $ArticleInfo['file'] =json_decode($ArticleInfo['file']);
                $ArticleInfo['tag']= explode(",",$ArticleInfo['tag']);
                $ArticleInfo['releaseAt'] = date('Y-m-d H:i:s',$ArticleInfo['releaseAt']);
                $json['data'] = $ArticleInfo;
                $json['success'] = true;
                $json['messages'] ='查询成功';

        }
      return json($json);
    }
    //修改权重
    public function weight(){
        $size = input('get.size');
        $page = input('get.page');
        $id = input('put.id');
        $weight = input('put.weight');
        $result = model('Articles')->where('id',$id)->find();
        if ($result){
            $result = model('Articles')->where('id',$id)->update(['weight'=>$weight]);
            if ($result){
                $return = $this->page($page,$size,true,'更改成功');

            }else{
                $return = $this->page($page,$size,false,'更改失败');
            }
        }else{
            $return = $this->page($page,$size,false,'查询不到该文章');
        }


        return json($return);
    }
}
